﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>支付宝支付(2) 电脑网站支付(SpringBoot+沙箱环境+Alipay Easy SDK)</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><h3><a id="_0"></a>一、前言</h3>
<ol>
<li><a href="https://zhengqing.blog.csdn.net/article/details/118575809">支付宝支付(1) 电脑网站支付(SpringBoot+沙箱环境)</a></li>
<li><a href="https://opendocs.alipay.com/open/54/00y8k9">Alipay Easy SDK 文档</a></li>
<li>Alipay Easy -&gt; <a href="https://github.com/alipay/alipay-easysdk">https://github.com/alipay/alipay-easysdk</a></li>
</ol>
<blockquote>
<p>注：本文只是简单的介绍Alipay Easy SDK的简单使用 <code>^_^</code></p>
</blockquote>
<h3><a id="_8"></a>二、获取所需配置参数信息</h3>
<blockquote>
<p>注：这个所需参数信息，参考下之前的文章即可，这里不多说</p>
</blockquote>
<ol>
<li>APPID</li>
<li>支付宝网关</li>
<li>支付宝公钥</li>
<li>RSA2私钥</li>
</ol>
<h3><a id="SpringBootDemo_17"></a>三、电脑网站支付-SpringBoot版Demo</h3>
<blockquote>
<p>可参考：<a href="https://github.com/alipay/alipay-easysdk/tree/master/java">https://github.com/alipay/alipay-easysdk/tree/master/java</a></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/20210712164821551.png" alt="在这里插入图片描述"></p>
<h4><a id="1pomxml_24"></a>1、<code>pom.xml</code>中新增依赖</h4>
<pre><code class="prism language-xml"><span class="token comment">&lt;!-- alipay-easysdk --&gt;</span>
<span class="token comment">&lt;!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alipay.sdk<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>alipay-easysdk<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.2.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<h4><a id="2applicationyml_36"></a>2、<code>application.yml</code>中添加配置</h4>
<blockquote>
<p>根据自己的配置修改即可</p>
</blockquote>
<pre><code class="prism language-yml"><span class="token key atrule">alipay</span><span class="token punctuation">:</span>
  <span class="token comment"># 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号</span>
  <span class="token key atrule">app_id</span><span class="token punctuation">:</span> xxx
  <span class="token comment"># 商户私钥，您的PKCS8格式RSA2私钥</span>
  <span class="token key atrule">merchant_private_key</span><span class="token punctuation">:</span> xxx
  <span class="token comment"># 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。</span>
  <span class="token key atrule">alipay_public_key</span><span class="token punctuation">:</span> xxx
  <span class="token comment"># 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数 [需外网能访问]</span>
  <span class="token key atrule">notify_url</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//mfn68i.natappfree.cc/error.html
  <span class="token comment"># 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数 [需外网能访问]</span>
  <span class="token key atrule">return_url</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//mfn68i.natappfree.cc/success
  <span class="token comment"># 签名方式</span>
  <span class="token key atrule">sign_type</span><span class="token punctuation">:</span> RSA2
  <span class="token comment"># 字符编码格式</span>
  <span class="token key atrule">charset</span><span class="token punctuation">:</span> utf<span class="token punctuation">-</span><span class="token number">8</span>
  <span class="token comment"># 支付宝网关</span>
  <span class="token key atrule">gatewayUrl</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//openapi.alipaydev.com/gateway.do
  <span class="token comment"># 日志路径</span>
  <span class="token key atrule">log_path</span><span class="token punctuation">:</span> <span class="token string">"log/"</span>
</code></pre>
<h4><a id="3_62"></a>3、配置类</h4>
<pre><code class="prism language-java"><span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@Configuration</span>
<span class="token annotation punctuation">@ConfigurationProperties</span><span class="token punctuation">(</span>prefix <span class="token operator">=</span> <span class="token string">"alipay"</span><span class="token punctuation">,</span> ignoreUnknownFields <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AlipayConfigProperty</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> app_id<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 商户私钥，您的PKCS8格式RSA2私钥
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> merchant_private_key<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> alipay_public_key<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> notify_url<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> return_url<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 签名方式
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> sign_type<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 字符编码格式
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> charset<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 支付宝网关
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> gatewayUrl<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 日志路径
     */</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> log_path<span class="token punctuation">;</span>

<span class="token punctuation">}</span>
</code></pre>
<h4><a id="4_118"></a>4、测试支付接口</h4>
<pre><code class="prism language-java"><span class="token annotation punctuation">@Slf4j</span>
<span class="token annotation punctuation">@Controller</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Api</span><span class="token punctuation">(</span>tags <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"测试支付接口"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">AlipayConfigProperty</span> alipayConfigProperty<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@PostConstruct</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">beforeInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 设置参数（全局只需设置一次）</span>
        <span class="token class-name">Factory</span><span class="token punctuation">.</span><span class="token function">setOptions</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/index"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"index.html"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// http://127.0.0.1:8080/alipay</span>
    <span class="token annotation punctuation">@ApiOperation</span><span class="token punctuation">(</span><span class="token string">"支付"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"alipay"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@ResponseBody</span>
    <span class="token annotation punctuation">@SneakyThrows</span><span class="token punctuation">(</span><span class="token class-name">Exception</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">alipay</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// 1、发起API调用（以电脑网站支付为例）</span>
            <span class="token comment">// 订单标题</span>
            <span class="token class-name">String</span> subject <span class="token operator">=</span> <span class="token string">"Apple iPhone11 128G"</span><span class="token punctuation">;</span>
            <span class="token comment">// 交易创建时传入的商户订单号</span>
            <span class="token class-name">String</span> outTradeNo <span class="token operator">=</span> <span class="token string">"012"</span><span class="token punctuation">;</span>
            <span class="token comment">// 订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]</span>
            <span class="token class-name">String</span> totalAmount <span class="token operator">=</span> <span class="token string">"1.00"</span><span class="token punctuation">;</span>
            <span class="token comment">// 支付成功后同步跳转的页面，是一个http/https开头的字符串</span>
            <span class="token class-name">String</span> returnUrl <span class="token operator">=</span> alipayConfigProperty<span class="token punctuation">.</span><span class="token function">getReturn_url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">AlipayTradePagePayResponse</span> alipayTradePagePayResponse <span class="token operator">=</span> <span class="token class-name">Factory<span class="token punctuation">.</span>Payment<span class="token punctuation">.</span>Page</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pay</span><span class="token punctuation">(</span>subject<span class="token punctuation">,</span> outTradeNo<span class="token punctuation">,</span> totalAmount<span class="token punctuation">,</span> returnUrl<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 2、处理响应或异常</span>
            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"[支付] 请求响应结果：\n {}"</span><span class="token punctuation">,</span> alipayTradePagePayResponse<span class="token punctuation">.</span><span class="token function">getBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> alipayTradePagePayResponse<span class="token punctuation">.</span><span class="token function">getBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"调用遭遇异常，原因："</span> <span class="token operator">+</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 设置支付宝配置参数
     *
     * @return 支付配置参数
     * @author zhengqingya
     * @date 2021/7/12 10:45
     */</span>
    <span class="token keyword">private</span> <span class="token class-name">Config</span> <span class="token function">getOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Config</span> config <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span>protocol <span class="token operator">=</span> <span class="token string">"https"</span><span class="token punctuation">;</span>
        <span class="token comment">// TODO 网关域名 线上为：openapi.alipay.com 沙箱为：openapi.alipaydev.com</span>
        config<span class="token punctuation">.</span>gatewayHost <span class="token operator">=</span> <span class="token string">"openapi.alipaydev.com"</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span>signType <span class="token operator">=</span> <span class="token string">"RSA2"</span><span class="token punctuation">;</span>

        config<span class="token punctuation">.</span>appId <span class="token operator">=</span> alipayConfigProperty<span class="token punctuation">.</span><span class="token function">getApp_id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中</span>
        config<span class="token punctuation">.</span>merchantPrivateKey <span class="token operator">=</span> alipayConfigProperty<span class="token punctuation">.</span><span class="token function">getMerchant_private_key</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载</span>
<span class="token comment">//        config.merchantCertPath = "&lt;-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt --&gt;";</span>
<span class="token comment">//        config.alipayCertPath = "&lt;-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt --&gt;";</span>
<span class="token comment">//        config.alipayRootCertPath = "&lt;-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt --&gt;";</span>

        <span class="token comment">//注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可</span>
        config<span class="token punctuation">.</span>alipayPublicKey <span class="token operator">=</span> alipayConfigProperty<span class="token punctuation">.</span><span class="token function">getAlipay_public_key</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//可设置异步通知接收服务地址（可选）</span>
        config<span class="token punctuation">.</span>notifyUrl <span class="token operator">=</span> alipayConfigProperty<span class="token punctuation">.</span><span class="token function">getNotify_url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//可设置AES密钥，调用AES加解密相关接口时需要（可选）</span>
<span class="token comment">//        config.encryptKey = "&lt;-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== --&gt;";</span>
        <span class="token keyword">return</span> config<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token annotation punctuation">@ApiOperation</span><span class="token punctuation">(</span><span class="token string">"支付成功回调"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"success"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@SneakyThrows</span><span class="token punctuation">(</span><span class="token class-name">Exception</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">alipaySuccess</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//获取支付宝GET过来反馈信息</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> params <span class="token operator">=</span> <span class="token class-name">Maps</span><span class="token punctuation">.</span><span class="token function">newHashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Map</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span> requestParams <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getParameterMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> name <span class="token operator">:</span> requestParams<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> values <span class="token operator">=</span> requestParams<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> valueStr <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> values<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                valueStr <span class="token operator">=</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> values<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">?</span> valueStr <span class="token operator">+</span> values<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
                        <span class="token operator">:</span> valueStr <span class="token operator">+</span> values<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">","</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//乱码解决，这段代码在出现乱码时使用</span>
            valueStr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>valueStr<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">"ISO-8859-1"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            params<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> valueStr<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 验签</span>
        <span class="token class-name">Boolean</span> signVerified <span class="token operator">=</span> <span class="token class-name">Factory<span class="token punctuation">.</span>Payment<span class="token punctuation">.</span>Common</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">verifyNotify</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 请在这里编写您的程序（以下代码仅作参考）——</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>signVerified<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//商户订单号</span>
            <span class="token class-name">String</span> out_trade_no <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">"out_trade_no"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">"ISO-8859-1"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//支付宝交易号</span>
            <span class="token class-name">String</span> trade_no <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">"trade_no"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">"ISO-8859-1"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//付款金额</span>
            <span class="token class-name">String</span> total_amount <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">"total_amount"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">"ISO-8859-1"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"[支付成功回调] 订单信息：trade_no:{} out_trade_no:{} total_amount:{}"</span><span class="token punctuation">,</span> trade_no<span class="token punctuation">,</span> out_trade_no<span class="token punctuation">,</span> total_amount<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token string">"success.html"</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"验签失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token string">"error.html"</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h4><a id="5_html_242"></a>5、回调成功/失败 html页面</h4>
<p>success.html</p>
<pre><code class="prism language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>utf-8<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>SUCCESS<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<p>error.html</p>
<pre><code class="prism language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>utf-8<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>ERROR<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<h4><a id="6_275"></a>6、运行</h4>
<p>调用支付接口 <a href="http://127.0.0.1:8080/alipay">http://127.0.0.1:8080/alipay</a></p>
<blockquote>
<p>温馨小提示：如果出现提示<code>支付存在钓鱼风险！</code> ，解决：换个浏览器清除所有缓存再次尝试即可！<br>
<img src="https://img-blog.csdnimg.cn/20210710154005866.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MjI1NTU4,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/20210712164148567.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MjI1NTU4,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>支付成功后，会回调我们配置的<code>return_url</code>地址，然后处理自己的业务即可…</p>
<h3><a id="demo_286"></a>四、本文案例demo源码</h3>
<p><a href="https://gitee.com/zhengqingya/java-workspace">https://gitee.com/zhengqingya/java-workspace</a></p>
<hr>
<blockquote>
<p>今日分享语句：<br>
世界上一切的成功，都将通过勤奋来实现。<br>
勤奋，代表着人们博弈人生的动力；<br>
成功，却是这种动力下产出的结果。<br>
没有勤奋，就没有成功，即便你是一个天资聪颖、淋漓尽致的人。</p>
</blockquote>
</div>
</body>

</html>
